package com.zp.business.controller.outpFees;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.clinicOrder.vo.ClinicOrdersVo;
import com.zp.business.entity.outpFees.bo.FeesParamBo;
import com.zp.business.entity.outpFees.bo.OutpFeesMasterBo;
import com.zp.business.entity.outpFees.vo.OutpFeesMasterVo;
import com.zp.business.service.outpFees.IOutpFeesMasterService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 门诊收费记录主控制器
 * 前端访问路由地址为:/finance/outpFeesMaster
 *
 * @author zhang peng
 * @ date 2024-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/outpFeesMaster")
public class OutpFeesMasterController extends BaseController {

    private final IOutpFeesMasterService iOutpFeesMasterService;

    /**
     * 查询门诊收费记录主列表 (分页)
     */
    @SaCheckPermission("finance:outpFeesMaster:list")
    @GetMapping("/selectPage")
    public TableDataInfo<OutpFeesMasterVo> selectPage (OutpFeesMasterBo bo, PageQuery pageQuery) {
        return iOutpFeesMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询门诊收费记录主列表 (不分页)
     */
    @SaCheckPermission("finance:outpFeesMaster:list")
    @GetMapping("/list")
    public R<List<OutpFeesMasterVo>> list (OutpFeesMasterBo bo) {
        List<OutpFeesMasterVo> list = iOutpFeesMasterService.queryList(bo);
        return R.ok(list);
    }

    @GetMapping("/listClinicOrders")
    public R<List<ClinicOrdersVo>> listClinicOrders (OutpFeesMasterBo bo) {
        List<ClinicOrdersVo> list = iOutpFeesMasterService.listClinicOrders(bo);
        return R.ok(list);
    }

    /**
     * 查询门诊退费申请记录
     */
    @GetMapping("/getRefundList")
    public R<OutpFeesMasterVo> getRefundList (OutpFeesMasterBo bo) {
        OutpFeesMasterVo outpFeesMasterVo = iOutpFeesMasterService.getRefundList(bo);
        return R.ok(outpFeesMasterVo);
    }

    /**
     * 导出门诊收费记录主列表
     */
    @SaCheckPermission("finance:outpFeesMaster:export")
    @Log(title = "门诊收费记录主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (OutpFeesMasterBo bo, HttpServletResponse response) {
        List<OutpFeesMasterVo> list = iOutpFeesMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "门诊收费记录主", OutpFeesMasterVo.class, response);
    }

    /**
     * 获取门诊收费记录主详细信息
     *
     * @param outpFeesMasterId 主键
     */
//    @SaCheckPermission("finance:outpFeesMaster:list")
    @GetMapping("/{outpFeesMasterId}")
    public R<OutpFeesMasterVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String outpFeesMasterId) {
        return R.ok(iOutpFeesMasterService.queryById(outpFeesMasterId));
    }

    /**
     * 新增门诊收费记录主
     */
//    @SaCheckPermission("finance:outpFeesMaster:add")
    @Log(title = "门诊收费记录主", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody FeesParamBo bo) {
        return toAjax(iOutpFeesMasterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改门诊收费记录主
     */
//    @SaCheckPermission("finance:outpFeesMaster:edit")
    @Log(title = "门诊收费记录主", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody OutpFeesMasterBo bo) {
        return toAjax(iOutpFeesMasterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除门诊收费记录主
     *
     * @param outpFeesMasterIds 主键串
     */
//    @SaCheckPermission("finance:outpFeesMaster:remove")
    @Log(title = "门诊收费记录主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{outpFeesMasterIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] outpFeesMasterIds) {
        return toAjax(iOutpFeesMasterService.deleteWithValidByIds(Arrays.asList(outpFeesMasterIds), true) ? 1 : 0);
    }

    /**
     * 修改收费状态
     *
     * @param id   feesNo或applyNo或applyItenId
     * @param type 单个退费和全部退费状态 (单个：single 全部：all)
     */
//    @SaCheckPermission("business:examApply:remove")
    @Log(title = "门诊收费记录主-修改收费状态", businessType = BusinessType.UPDATE)
    @GetMapping("/refundApplication/{id}/{chargeFlag}/{type}/{orgId}/{feesNo}")
    public R<Boolean> refundApplication (@NotEmpty(message = "信息不能为空") @PathVariable String id,
                                         @NotEmpty(message = "收费类型不能为空") @PathVariable String chargeFlag,
                                         @NotEmpty(message = "单个退费和全部退费状态不能为空") @PathVariable String type,
                                         @NotEmpty(message = "单机构id不能为空") @PathVariable String orgId,
                                         @NotEmpty(message = "收费号不能为空") @PathVariable String feesNo
    ) {
        return R.ok(iOutpFeesMasterService.refundApplication(id, chargeFlag, type, orgId, feesNo));
    }

    @PostMapping("/confirmationRefund")
    public R<Boolean> confirmationRefund (@RequestBody FeesParamBo bo) {
        return R.ok(iOutpFeesMasterService.confirmationRefund(bo));
    }
}
